<?php

namespace App\Http\Controllers;

use App\Models\Certification;
use App\Models\Country;
use App\Models\Course;
use App\Models\Exam;
use App\Models\Group;
use App\Models\GroupActivities;
use App\Models\GroupExam;
use App\Models\GroupReview;
use App\Models\Journal;
use App\Models\Question;
use App\Models\SessionGroup;
use App\Traits\DataTable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class ParticipantSpaceController extends Controller
{
    use DataTable;

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function groups(Request $request) 
    {
        $trashed = $request->segment(count(request()->segments()));
        $options = [
            'breadcrumb' => ['لوحة التحكم', 'المجموعات'],
            'route' => route('user.groups'),
            'buttons' => [
                'delete' => 'groups.multiDelete',
                'restore' => 'groups.multiRestore',
                'forceDelete' => 'groups.multiForceDelete',
            ],
            'trashed' => $trashed,
            /* 'create' => [
                'route' => 'groups.create',
                'title' => 'إضافة مجموعة',
            ], */
            'search' => true,
            'export' => true,
            'filters' => [
                [
                    'name' => 'country',
                    'type' => 'select',
                    'title' => 'الدولة',
                    'all' => 'كل الدول',
                    'column' => 3,
                    'data' => Country::pluck('name', 'id')->toArray(),
                    'get' => [
                        'ajax' => route('ajax.citiesFromCountry', ':id'),
                        'empty' => [
                            'city',
                            'hotel',
                        ],
                        'select' => [
                            'city',
                        ]
                    ],
                ],
                [
                    'name' => 'city',
                    'type' => 'select',
                    'title' => 'المدينة',
                    'all' => 'كل المدن',
                    'column' => 4,
                    'data' => [],
                    'get' => [
                        'ajax' => route('ajax.hotelsFromCity', ':id'),
                        'empty' => [
                            'hotel',
                        ],
                        'select' => [
                            'hotel',
                        ]
                    ],
                ],
                [
                    'name' => 'hotel',
                    'type' => 'select',
                    'title' => 'الفندق',
                    'all' => 'كل الفنادق',
                    'column' => 5,
                    'data' => [],
                    'get' => [
                        'ajax' => route('ajax.venuesFromHotel', ':id'),
                        'empty' => [
                            'venue',
                        ],
                        'select' => [
                            'venue',
                        ]
                    ],
                ],
                [
                    'name' => 'venue',
                    'type' => 'select',
                    'title' => 'القاعة',
                    'all' => 'كل القاعات',
                    'column' => 6,
                    'data' => [],
                ],
                [
                    'name' => 'course',
                    'type' => 'select',
                    'title' => 'الدورة',
                    'all' => 'كل الدورات',
                    'column' => 7,
                    'data' => Course::pluck('name', 'id')->toArray(),
                ]
            ],
            'defaultOrder' => [
                [1, 'desc']
            ],
            'columns' => [
                [
                    'title' => 'Checkbox',
                    'id' => 'checkbox',
                    'width' => 5,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'checkbox' => []
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الرقم',
                    'id' => 'id',
                    'width' => 5,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'id',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الاسم',
                    'id' => 'name',
                    'width' => 20,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الدولة',
                    'id' => 'country_id',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'venue->hotel->city->country->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'المدينة',
                    'id' => 'city_id',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'venue->hotel->city->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'تاريخ البداية',
                    'id' => 'start_date',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'start_date',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'تاريخ النهاية',
                    'id' => 'end_date',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'end_date',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الفندق',
                    'id' => 'hotel_id',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'venue->hotel->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'القاعة',
                    'id' => 'venue_id',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'venue->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الدورة',
                    'id' => 'course_id',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'course->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'عدد المشاركين',
                    'id' => 'participants_count',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'participants_count',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الإعدادات',
                    'id' => 'settings',
                    'width' => 10,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'settings' => [
                                    [/* 
                                        [
                                            'title' => 'الحصص',
                                            'route' => 'groups.participants.presents.index',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.participants.presents.index',
                                            'type' => 'directly',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'الجلسات',
                                            'route' => 'groups.sessions.index',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.sessions.index',
                                            'type' => 'directly',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'الانشطة',
                                            'route' => 'groups.activities.index',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.activities.index',
                                            'type' => 'directly',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'الاختبارات',
                                            'route' => 'groups.exams.index',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.exams.index',
                                            'type' => 'directly',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'التقييمات',
                                            'route' => 'groups.reviews.index',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.reviews.index',
                                            'type' => 'directly',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'نظام التنبيه',
                                            'route' => 'groups.alert',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.alert',
                                            'type' => 'popup',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'البطاقات و الاهرام',
                                            'route' => 'groups.export.cards',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.export.cards',
                                            'type' => 'popup',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'الشهادات',
                                            'route' => 'groups.certifications.index',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.certifications.index',
                                            'type' => 'directly',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'تعديل',
                                            'route' => 'groups.edit',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.edit',
                                            'type' => 'popup',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف',
                                            'route' => 'groups.destroy',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.destroy',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف المجموعة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'استعادة',
                                            'route' => 'groups.restore',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.restore',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من استعادة المجموعة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف نهائي',
                                            'route' => 'groups.forceDelete',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.forceDelete',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف المجموعة نهائيا؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ]
                                     */]
                                ]
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ]
            ]
        ];

        if($request->ajax == true) {
            $participantId = Auth::user()->participant()->first()->id;

            $data = Group::withTrashed()->where(function ($query) use ($trashed) {
                if ($trashed == 'trashed') {
                    $query->onlyTrashed();
                } else {
                    $query->withoutTrashed();
                }
            })
            ->whereHas('participants', function ($query) use ($participantId) {
                $query->where('participant_id', $participantId);
            })
            ->with(['venue', 'venue.hotel', 'venue.hotel.city', 'venue.hotel.city.country', 'course'])->withCount('participants')->get()->toArray();

            return $this->init($data, $options['columns']);
        }

        return view('data', compact(
            'options'
        ));
    }

    public function sessions(Request $request) 
    {
        $trashed = $request->segment(count(request()->segments()));
        // dump($trashed);
        $options = [
            'breadcrumb' => ['لوحة التحكم', 'الجلسات'],
            'route' => route('user.sessions'),
            'buttons' => [
                // 'delete' => 'groups.sessions.multiDelete',
                // 'restore' => 'groups.sessions.multiRestore',
                // 'forceDelete' => 'groups.sessions.multiForceDelete',
            ],
            // 'trashed' => $trashed,
            /* 'create' => [
                'route' => route('groups.sessions.create',['id'=>$id]),
                'permission' => 'groups.sessions.create',
                'title' => 'إضافة جلسة',
            ], */

            

            'search' => true,
            'export' => true,
            /* 'filters' => [
                [
                    'name' => 'country',
                    'type' => 'select',
                    'title' => 'الدولة',
                    'all' => 'كل الدول',
                    'column' => 3,
                    'data' => Country::pluck('name', 'id')->toArray()
                ]
            ], */
            'defaultOrder' => [
                [1, 'desc']
            ],
            'columns' => [
                [
                    'title' => 'Checkbox',
                    'id' => 'checkbox',
                    'width' => 5,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'checkbox' => []
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الرقم',
                    'id' => 'id',
                    'width' => 5,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'id',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'المجموعة',
                    'id' => 'group',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'group->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'التاريخ',
                    'id' => 'name',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'date',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'وقت البداية',
                    'id' => 'start_time',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'start_time',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'وقت البداية',
                    'id' => 'end_time',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'end_time',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'النوع',
                    'id' => 'type',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'type',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'القاعة',
                    'id' => 'venue',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'venue->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الإعدادات',
                    'id' => 'settings',
                    'width' => 8,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'settings' => [
                                    [
                                        [
                                            'title' => 'تعديل',
                                            'route' => 'groups.sessions.edit',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.sessions.edit',
                                            'type' => 'popup',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف',
                                            'route' => 'groups.sessions.destroy',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.sessions.destroy',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الجلسة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'استعادة',
                                            'route' => 'groups.sessions.restore',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.sessions.restore',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من استعادة الجلسة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف نهائي',
                                            'route' => 'groups.sessions.forceDelete',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.sessions.forceDelete',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الجلسة نهائيا؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ]
            ]
        ];


        if($request->ajax == true) {
            $participantId = Auth::user()->participant()->first()->id;
            
            $data = SessionGroup::withTrashed()->where(function ($query) use ($trashed) {
                if ($trashed == 'trashed') {
                    $query->onlyTrashed();
                } else {
                    $query->withoutTrashed();
                }
            })
            ->whereHas('group.participants', function ($query) use ($participantId) {
                $query->where('participant_id', $participantId);
            })
            ->with('group')
            ->with('venue')
            ->get()->toArray();
            
            return $this->init($data, $options['columns']);
        }

        return view('data', compact(
            'options'
        ));
    }

    public function activities(Request $request) 
    {
        $trashed = $request->segment(count(request()->segments()));
        $options = [
            'breadcrumb' => ['لوحة التحكم', 'الأنشطة'],
            'route' => route('user.activities'),
            'buttons' => [
                'delete' => 'groups.activities.multiDelete',
                'restore' => 'groups.activities.multiRestore',
                'forceDelete' => 'groups.activities.multiForceDelete',
            ],
            'trashed' => $trashed,
            /* 'create' => [
                'route' => route('groups.activities.create',['id'=>$id]),
                'permission' => 'groups.activities.create',
                'title' => 'إضافة نشاط',
            ], */

            /* 'trashed_link' => [
                'route' => route('groups.activities.trashed',['id'=>$id]),
                'permission' => 'groups.activities.trashed',
                'title' => 'الأنشطة المحدوفة',
            ],
            'list_link' => [
                'route' => route('groups.activities.index',['id'=>$id]),
                'permission' => 'groups.activities.index',
                'title' => 'الأنشطة',
            ], */
            'search' => true,
            'export' => true,
            /* 'filters' => [
                [
                    'name' => 'country',
                    'type' => 'select',
                    'title' => 'الدولة',
                    'all' => 'كل الدول',
                    'column' => 3,
                    'data' => Country::pluck('name', 'id')->toArray()
                ]
            ], */
            'defaultOrder' => [
                [1, 'desc']
            ],
            'columns' => [
                [
                    'title' => 'Checkbox',
                    'id' => 'checkbox',
                    'width' => 5,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'checkbox' => []
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الرقم',
                    'id' => 'id',
                    'width' => 5,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'id',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'المجموعة',
                    'id' => 'group',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'group->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'النشاط',
                    'id' => 'activity',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'activity->title',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'تاريخ البداية',
                    'id' => 'start_date',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'start_date',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'تاريخ النهاية',
                    'id' => 'end_date',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'end_date',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'وقت البداية',
                    'id' => 'start_time',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'start_time',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'وقت البداية',
                    'id' => 'end_time',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'end_time',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الإعدادات',
                    'id' => 'settings',
                    'width' => 8,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'settings' => [
                                    [
                                        [
                                            'title' => 'تعديل',
                                            'route' => 'groups.activities.edit',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.activities.edit',
                                            'type' => 'popup',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف',
                                            'route' => 'groups.activities.destroy',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.activities.destroy',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الجلسة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'استعادة',
                                            'route' => 'groups.activities.restore',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.activities.restore',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من استعادة الجلسة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف نهائي',
                                            'route' => 'groups.activities.forceDelete',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.activities.forceDelete',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الجلسة نهائيا؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ]
            ]
        ];

        if($request->ajax == true) {
            $participantId = Auth::user()->participant()->first()->id;
            $data = GroupActivities::withTrashed()->where(function ($query) use ($trashed) {
                if ($trashed == 'trashed') {
                    $query->onlyTrashed();
                } else {
                    $query->withoutTrashed();
                }
            })
            ->whereHas('group.participants', function ($query) use ($participantId) {
                $query->where('participant_id', $participantId);
            })
            ->with('activity')
            ->get()->toArray();

            return $this->init($data, $options['columns']);
        }

        return view('data', compact(
            'options'
        ));
    }

    public function exams(Request $request) 
    {
        $trashed = $request->segment(count(request()->segments()));
        $options = [
            'breadcrumb' => ['لوحة التحكم', 'الاختبارات'],
            'route' => route('user.exams'),
            'buttons' => [
                'delete' => 'groups.exams.multiDelete',
                'restore' => 'groups.exams.multiRestore',
                'forceDelete' => 'groups.exams.multiForceDelete',
            ],
            'trashed' => $trashed,
            /* 'create' => [
                'route' => route('groups.exams.create',['id'=>$id]),
                'permission' => 'groups.exams.create',
                'title' => 'إضافة اختبار',
            ], */

            /* 'trashed_link' => [
                'route' => route('groups.exams.trashed',['id'=>$id]),
                'permission' => 'groups.exams.trashed',
                'title' => 'الاختبارات المحدوفة',
            ],
            'list_link' => [
                'route' => route('groups.exams.index',['id'=>$id]),
                'permission' => 'groups.exams.index',
                'title' => 'الاختبارات',
            ], */
            'search' => true,
            'export' => true,
            /* 'filters' => [
                [
                    'name' => 'country',
                    'type' => 'select',
                    'title' => 'الدولة',
                    'all' => 'كل الدول',
                    'column' => 3,
                    'data' => Country::pluck('name', 'id')->toArray()
                ]
            ], */
            'defaultOrder' => [
                [1, 'desc']
            ],
            'columns' => [
                [
                    'title' => 'Checkbox',
                    'id' => 'checkbox',
                    'width' => 5,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'checkbox' => []
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الرقم',
                    'id' => 'id',
                    'width' => 5,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'id',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'المجموعة',
                    'id' => 'group',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'group->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الاختبارات',
                    'id' => 'exam',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'exam->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'تاريخ البداية',
                    'id' => 'start_date',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'start_date',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'تاريخ النهاية',
                    'id' => 'end_date',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'end_date',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'وقت البداية',
                    'id' => 'start_time',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'start_time',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'وقت البداية',
                    'id' => 'end_time',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'end_time',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الإعدادات',
                    'id' => 'settings',
                    'width' => 8,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'settings' => [
                                    [
                                        [
                                            'title' => 'تعديل',
                                            'route' => 'groups.exams.edit',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.exams.edit',
                                            'type' => 'popup',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف',
                                            'route' => 'groups.exams.destroy',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.exams.destroy',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الجلسة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'استعادة',
                                            'route' => 'groups.exams.restore',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.exams.restore',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من استعادة الجلسة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف نهائي',
                                            'route' => 'groups.exams.forceDelete',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.exams.forceDelete',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الجلسة نهائيا؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ]
            ]
        ];

        if($request->ajax == true) {
            $participantId = Auth::user()->participant()->first()->id;
            $data = GroupExam::withTrashed()->where(function ($query) use ($trashed) {
                if ($trashed == 'trashed') {
                    $query->onlyTrashed();
                } else {
                    $query->withoutTrashed();
                }
            })
            ->whereHas('group.participants', function ($query) use ($participantId) {
                $query->where('participant_id', $participantId);
            })
            ->with('exam')
            ->with('group')
            ->get()->toArray();

            return $this->init($data, $options['columns']);
        }

        return view('data', compact(
            'options'
        ));
    }

    public function reviews(Request $request) 
    {
        $trashed = $request->segment(count(request()->segments()));
        $options = [
            'breadcrumb' => ['لوحة التحكم', 'التقييمات'],
            'route' => route('user.reviews'),
            /* 'buttons' => [
                'delete' => 'groups.reviews.multiDelete',
                'restore' => 'groups.reviews.multiRestore',
                'forceDelete' => 'groups.reviews.multiForceDelete',
            ],
            'trashed' => $trashed,
            'create' => [
                'route' => route('groups.reviews.create',['id'=>$id]),
                'permission' => 'groups.reviews.create',
                'title' => 'إضافة تقييم',
            ], */

            /* 'trashed_link' => [
                'route' => route('groups.reviews.trashed',['id'=>$id]),
                'permission' => 'groups.reviews.trashed',
                'title' => 'التقييمات المحدوفة',
            ],
            'list_link' => [
                'route' => route('groups.reviews.index',['id'=>$id]),
                'permission' => 'groups.reviews.index',
                'title' => 'التقييمات',
            ], */
            'search' => true,
            'export' => true,
            /* 'filters' => [
                [
                    'name' => 'country',
                    'type' => 'select',
                    'title' => 'الدولة',
                    'all' => 'كل الدول',
                    'column' => 3,
                    'data' => Country::pluck('name', 'id')->toArray()
                ]
            ], */
            'defaultOrder' => [
                [1, 'desc']
            ],
            'columns' => [
                [
                    'title' => 'Checkbox',
                    'id' => 'checkbox',
                    'width' => 5,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'checkbox' => []
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الرقم',
                    'id' => 'id',
                    'width' => 5,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'id',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'المجموعة',
                    'id' => 'group',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'group->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'التقييم',
                    'id' => 'review',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'review->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'التاريخ ',
                    'id' => 'date',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'date',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الوقت',
                    'id' => 'time',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'time',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الإعدادات',
                    'id' => 'settings',
                    'width' => 8,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'settings' => [
                                    [
                                        [
                                            'title' => 'تقييمات المستخدمين',
                                            'route' => 'groups.reviews.edit',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.reviews.edit',
                                            'type' => 'directly',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'تعديل',
                                            'route' => 'groups.reviews.edit',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.reviews.edit',
                                            'type' => 'popup',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف',
                                            'route' => 'groups.reviews.destroy',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.reviews.destroy',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الجلسة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'استعادة',
                                            'route' => 'groups.reviews.restore',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.reviews.restore',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من استعادة الجلسة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف نهائي',
                                            'route' => 'groups.reviews.forceDelete',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.reviews.forceDelete',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الجلسة نهائيا؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ]
            ]
        ];

        if($request->ajax == true) {
            $participantId = Auth::user()->participant()->first()->id;
            $data = GroupReview::withTrashed()->where(function ($query) use ($trashed) {
                if ($trashed == 'trashed') {
                    $query->onlyTrashed();
                } else {
                    $query->withoutTrashed();
                }
            })
            ->whereHas('group.participants', function ($query) use ($participantId) {
                $query->where('participant_id', $participantId);
            })
            ->with('group')
            ->with('review')
            ->get()->toArray();

            return $this->init($data, $options['columns']);
        }

        return view('data', compact(
            'options'
        ));
    }

    public function certifications(Request $request) 
    {

        // $group_id = $id;//$request->input('id');
        $trashed = $request->segment(count(request()->segments()));
        $options = [
            'breadcrumb' => ['لوحة التحكم', 'الشهادات'],
            'route' => route('user.certifications'),
            'buttons' => [
                /* 'delete' => 'groups.certifications.multiDelete',
                'restore' => 'groups.certifications.multiRestore',
                'forceDelete' => 'groups.certifications.multiForceDelete', */
            ],
            /* 'trashed' => $trashed,
            'create' => [
                'route' => route('certifications.groups.export',['id'=>$group_id]),
                'permission' => 'groups.certifications.export',
                'title' => 'استخراج الشهادات',
            ],
            'trashed_link' => [
                'route' => route('groups.certifications.trashed',['id'=>$id]),
                'permission' => 'groups.certifications.trashed',
                'title' => 'الشهادات المحدوفة',
            ],
            'list_link' => [
                'route' => route('groups.certifications.index',['id'=>$id]),
                'permission' => 'groups.certifications.index',
                'title' => 'الشهادات',
            ], */
            'search' => true,
            'export' => true,
            'defaultOrder' => [
                [1, 'desc']
            ],
            'columns' => [
                [
                    'title' => 'Checkbox',
                    'id' => 'checkbox',
                    'width' => 5,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'checkbox' => []
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الرقم',
                    'id' => 'id',
                    'width' => 5,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'id',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'المجموعة',
                    'id' => 'group',
                    'width' => 8,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'group->name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الرقم التسلسلي',
                    'id' => 'id_cert',
                    'width' => 5,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'id_cert',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'رقم الاعتماد',
                    'id' => 'accreditation_number',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'accreditation_number',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'اسم المشترك',
                    'id' => 'name',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'name',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الشركة',
                    'id' => 'company',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'company',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الجنسية',
                    'id' => 'nationality',
                    'width' => 10,
                    'orderable' => true,
                    'exportable' => true,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'nationality',
                            'functions' => [],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ],
                [
                    'title' => 'الإعدادات',
                    'id' => 'settings',
                    'width' => 10,
                    'orderable' => false,
                    'exportable' => false,
                    'commas' => '',
                    'data' => [
                        [
                            'column' => 'allColumns',
                            'functions' => [
                                'settings' => [
                                    [
                                        /* [
                                            'title' => 'تعديل',
                                            'route' => 'groups.certifications.edit',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.certifications.edit',
                                            'type' => 'popup',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ], */
                                        [
                                            'title' => 'استخراج الشهادة',
                                            'route' => 'certifications.groups.export.one',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.certifications.export.one',
                                            'type' => 'directly',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف',
                                            'route' => 'groups.certifications.destroy',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.certifications.destroy',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الشهادة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '==',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'استعادة',
                                            'route' => 'groups.certifications.restore',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.certifications.restore',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من استعادة الشهادة؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ],
                                        [
                                            'title' => 'حذف نهائي',
                                            'route' => 'groups.certifications.forceDelete',
                                            'id' => 'inside',
                                            'columnId' => 'id',
                                            'permission' => 'groups.certifications.forceDelete',
                                            'type' => 'sweet_alert',
                                            'sweetAlert' => 'هل أنت متأكد من حذف الشهادة نهائيا؟',
                                            'conditions' => [
                                                [
                                                    'column' => 'deleted_at',
                                                    'type' => '!=',
                                                    'value' => [null]
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            'before' => '',
                            'after' => '',
                        ]
                    ]
                ]
            ]
        ];

        if($request->ajax == true) {
            $participantId = Auth::user()->participant()->first()->id;
            $data = Certification::withTrashed()->where(function ($query) use ($trashed) {
                if ($trashed == 'trashed') {
                    $query->onlyTrashed();
                } else {
                    $query->withoutTrashed();
                }
            })
            ->whereHas('group.participants', function ($query) use ($participantId) {
                $query->where('participant_id', $participantId);
            })
            ->with('group')
            ->get()->toArray();

            return $this->init($data, $options['columns']);
        }

        return view('data', compact(
            'options'
        ));
    }
}
